# -*- coding: utf-8 -*-
import sqlalchemy
from sqlalchemy.orm import sessionmaker

from ContractAdapter import ContractAdapter
from QuoteAdapter    import QuoteAdapter
from MarketDepthItem import MarketDepthItem
from Tick            import Tick


#-------------------------------------------------------------------------
# The Database wrapper class
# This class provides the class typed interface to the customer database
#-------------------------------------------------------------------------
class DBWrapper:
    
    #---------------------------------------------------------------------
    # Initialise the class with the database path and create the session    
    #---------------------------------------------------------------------
    def __init__(self, dbpath): 
       self.engine = sqlalchemy.create_engine(dbpath)
       self.session = sessionmaker(bind=self.engine)()

    #---------------------------------------------------------------------
    # Add an object to the database    
    #---------------------------------------------------------------------
    def add(self, obj):            
        self.session.add(obj)
        self.session.commit() 
        
    #---------------------------------------------------------------------
    # Add a tick to the database    
    #---------------------------------------------------------------------
    def addTick(self, tick):            
        self.session.add(Tick(tick))
        self.session.commit() 
        
    #---------------------------------------------------------------------
    # Add a quote to the database    
    #---------------------------------------------------------------------
    def addQuote(self, quote):            
        self.session.add(QuoteAdapter(quote))
        self.session.commit() 

    #---------------------------------------------------------------------
    # Add a mktdepth item to the database    
    #---------------------------------------------------------------------
    def addMktDepthItem(self, mktdepth):            
        self.session.add(MarketDepthItem(mktdepth))
        self.session.commit() 

    #---------------------------------------------------------------------
    # Get MarketDepthItems to the database    
    #---------------------------------------------------------------------
    def getMarketDepthItems(self):            
        return self.session.query(MarketDepthItem)
    
    #---------------------------------------------------------------------
    # Get Quotes from the database    
    #---------------------------------------------------------------------
    def getQuotes(self):            
        return [c.Quote() for c in self.session.query(QuoteAdapter)]
    
    #---------------------------------------------------------------------
    # Get a list of all Contracts
    #---------------------------------------------------------------------
    def getContracts(self):
        return [c.Contract() for c in self.session.query(ContractAdapter)]
    
    
    def __del__(self):           
        self.session.close_all()